package com.tyk.bookstore.back.platform.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tyk.bookstore.back.common.core.model.constant.OssConstant;
import com.tyk.bookstore.back.common.core.util.AssertUtil;
import com.tyk.bookstore.back.common.core.util.IdGenerator;
import com.tyk.bookstore.back.common.core.util.ObjUtil;
import com.tyk.bookstore.back.common.other.oss.manager.OssManager;
import com.tyk.bookstore.back.platform.model.entity.CarouselDo;
import com.tyk.bookstore.back.platform.model.query.CarouselAddQuery;
import com.tyk.bookstore.back.platform.model.query.CarouselPageQuery;
import com.tyk.bookstore.back.platform.model.query.CarouselUpdateQuery;
import com.tyk.bookstore.back.platform.model.result.CarouselResultEnum;
import com.tyk.bookstore.back.platform.model.vo.CarouselPageVo;
import com.tyk.bookstore.back.platform.model.vo.CarouselVo;
import com.tyk.bookstore.back.platform.repository.CarouselRepository;
import com.tyk.bookstore.back.platform.service.CarouselService;
import com.tyk.bookstore.back.platform.util.MapStructPlatform;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Set;

/**
 * @author tyk
 * @description 轮播图管理
 */
@Service
@RequiredArgsConstructor
public class CarouselServiceImp implements CarouselService {
    private final CarouselRepository carouselRepository;
    private final OssManager ossManager;
    private final MapStructPlatform mapStructPlatform;

    @Override
    public CarouselVo getById(Long id) {
        CarouselDo carousel = carouselRepository.selectById(id);
        AssertUtil.notNull(carousel, CarouselResultEnum.CAROUSEL_NOT_FOUND);
        return mapStructPlatform.toCarouselVo(carousel);
    }

    @Override
    public CarouselPageVo getPage(CarouselPageQuery query) {
        Page<CarouselDo> page = new Page<>(query.getPage(), query.getLimit());
        LambdaQueryWrapper<CarouselDo> w = buildPageWrapper(query);
        carouselRepository.selectPage(page, w);
        return mapStructPlatform.toCarouselPageVo(page);
    }


    @Override
    public void add(CarouselAddQuery query) {
        // 轮播图地址不存在
        AssertUtil.isFalse(
                carouselRepository.exist1Eq(CarouselDo::getPath, query.getPath(), CarouselDo.class),
                CarouselResultEnum.CAROUSEL_ALREADY_EXISTS
        );
        // 添加轮播图
        CarouselDo carousel = mapStructPlatform.toCarouselDo(query);
        carousel.setId(IdGenerator.id());
        carouselRepository.insert(carousel);
    }

    @Override
    public void updateById(CarouselUpdateQuery query) {
        // 轮播图存在
        AssertUtil.isTrue(
                carouselRepository.exist1Eq(CarouselDo::getId, query.getId(), CarouselDo.class),
                CarouselResultEnum.CAROUSEL_NOT_FOUND
        );
        // 轮播图地址不存在
        AssertUtil.isFalse(
                carouselRepository.exist1Eq1Ne(
                        CarouselDo::getPath, query.getPath(),
                        CarouselDo::getId, query.getId(),
                        CarouselDo.class),
                CarouselResultEnum.CAROUSEL_ALREADY_EXISTS
        );
        carouselRepository.updateById(mapStructPlatform.toCarouselDo(query));
    }

    @Override
    public void deleteById(Long id) {
        AssertUtil.isTrue(
                carouselRepository.exist1Eq(CarouselDo::getId, id, CarouselDo.class),
                CarouselResultEnum.CAROUSEL_NOT_FOUND
        );
        carouselRepository.deleteById(id);
    }

    @Override
    public void deleteBatchIdSet(Set<Long> idSet) {
        AssertUtil.equal(
                carouselRepository.count1In(CarouselDo::getId, idSet, CarouselDo.class),
                (long) idSet.size(),
                CarouselResultEnum.CAROUSEL_NOT_FOUND
        );
        carouselRepository.deleteBatchIds(idSet);
    }

    @Override
    public String upload(MultipartFile file) {
        return ossManager.upload(file, OssConstant.PRODUCT_CAROUSEL);
    }

    @Override
    public List<String> multiUpload(List<MultipartFile> fileList) {
        return fileList.stream().map(file -> ossManager.upload(file, OssConstant.PRODUCT_CAROUSEL)).toList();
    }

    /**
     * 构建分页查询Wrapper
     *
     * @param query 参数
     * @return Wrapper
     */
    private static LambdaQueryWrapper<CarouselDo> buildPageWrapper(CarouselPageQuery query) {
        LambdaQueryWrapper<CarouselDo> w = Wrappers.lambdaQuery(CarouselDo.class);

        w.ge(query.getPriorityFrom() != null, CarouselDo::getPriority, query.getPriorityFrom());
        w.le(query.getPriorityTo() != null, CarouselDo::getPriority, query.getPriorityTo());

        w.eq(query.getState() != null, CarouselDo::getState, query.getState());

        w.ge(query.getStartTime() != null, CarouselDo::getCreateTime, query.getStartTime());
        w.le(query.getEndTime() != null, CarouselDo::getCreateTime, query.getEndTime());

        w.like(ObjUtil.notBlank(query.getKeyword()), CarouselDo::getDescription, query.getKeyword());

        w.orderBy(true, false, CarouselDo::getCreateTime);

        return w;
    }

}
